window.addEventListener('load', function () {
    let goRegister = this.document.querySelector('.goRegister')
    let btnlogin = this.document.querySelector('.btnlogin')
    let register = this.document.querySelector('.register')
    let login = this.document.querySelector('.login')
    let goLogin = this.document.querySelector('.goLogin')
    let regUserName = this.document.querySelector('.register [name="username"]')
    let redPassword = this.document.querySelector('.register [name="password"]')

    let logUserName = this.document.querySelector('.login [name="username"]')
    let logPassword = this.document.querySelector('.login [name="password"]')
    goRegister.addEventListener('click', function () {

        register.style.display = 'block'
        login.style.display = 'none'
    })
    goLogin.addEventListener('click', function () {

        register.style.display = 'none'
        login.style.display = 'block'
    })
    //二: 实现登录注册的校验
    // 比如，验证一个用户名和密码
    function test() {
        return {
            fields: {
                username: { // 这里username是 input 的name属性值，表示对这个输入框进行验证
                    validators: { // 添加真正的校验规则
                        notEmpty: {   //不能为空
                            message: '用户名不能为空.'// 如果不满足校验规则，则给出这句提示
                        },
                        stringLength: {   //检测长度
                            min: 2, // 最少2位
                            max: 15, // 最多15位
                            message: '用户名需要2~15个字符'
                        }
                    }
                },
                password: {
                    validators: {
                        notEmpty: {
                            message: '密码不能为空'
                        },
                        stringLength: {   //检测长度
                            min: 6,
                            max: 15,
                            message: '密码需要6~15个字符'
                        }
                    }
                }
            }
        }
    }
    $('.register form')
        .bootstrapValidator(test())
        .on('success.form.bv', function (e) {
            e.preventDefault();
            axios({
                url: '/api/register',
                method: 'post',
                data: {
                    username: regUserName.value,
                    password: redPassword.value
                }

            }).then(res => {
                // console.log(res);
                if (res.data.code == 0) {
                    toastr.success(res.data.message)
                    //返回登录页面
                    register.style.display = 'none'
                    login.style.display = 'block'
                } else if (res.data.code == 1) {
                    toastr.warning(res.data.message)
                }
            })
        })

    $('.login form')
        .bootstrapValidator(test())
        .on('success.form.bv', function (e) {
            e.preventDefault();
            axios({
                url: '/api/login',
                method: 'post',
                data: {
                    username: logUserName.value,
                    password: logPassword.value
                }

            }).then(res => {
                if (res.data.code == 0) {
                    // alert(res.data.message)
                    //本地存储token
                    localStorage.setItem('83token', res.data.token)
                    //成功提示
                    toastr.success(res.data.message)
                    location.href = './index.html'

                } else if (res.data.code == 1) {
                    // 错误提示
                    toastr.warning(res.data.message)
                }
            })
        })











})